package br.ufpe.cin.xmldbdev.negocio;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

import br.ufpe.cin.xmldbdev.dados.CadastroConexao;
import br.ufpe.cin.xmldbdev.modelo.Conexao;

public class ControladorConexao {
	
	private CadastroConexao cadastroConexao;
	
	public ControladorConexao() {
		cadastroConexao = new CadastroConexao();
		try {
			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void inserirConexao(Conexao c) throws Exception {
		cadastroConexao.inserirConexao(c);
	}
	
	public List<Conexao> getConexoes(){
		return cadastroConexao.getConexoes();
	}
	
	public void salvarConexoes(){
		try {
			cadastroConexao.salvarConexoes();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public boolean testarConexao(Conexao c) {
		boolean result = false;
		String conexString = "jdbc:oracle:thin:@" + c.getNomeHost() + ":" + c.getPorta() + ":xe";
		try {
			Connection con = DriverManager.getConnection(conexString, c.getNomeUsuario(), c.getSenha());
			if (con != null)
				result = true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}
	
	public Connection getConnection(Conexao c) {
		Connection conn = null;
		String conexString = "jdbc:oracle:thin:@" + c.getNomeHost() + ":" + c.getPorta() + ":xe";
		try {
			conn = DriverManager.getConnection(conexString, c.getNomeUsuario(), c.getSenha());
		} catch (Exception e) {
			// TODO: handle exception
		}
		return conn;
	}

}
